-- ~/.config/nvim/lua/plugins/init.lua
vim.api.nvim_set_keymap("n", "<F2>", ":NvimTreeToggle<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<leader>ff", ":NvimTreeFindFile<CR>:wincmd p<CR>", { noremap = true, silent = true })

-- 启动时自动打开 nvim-tree
vim.api.nvim_create_autocmd("VimEnter", {
	pattern = "*",
	callback = function()
		vim.cmd("NvimTreeToggle") -- 执行第一条命令
		vim.cmd("wincmd p") -- 执行第二条命令, 跳转到buffer
	end,
})
-- 仅剩 nvim-tree 窗口时自动退出 Neovim
vim.api.nvim_create_autocmd("BufEnter", {
	nested = true,
	callback = function()
		if #vim.api.nvim_list_wins() == 1 and vim.api.nvim_buf_get_name(0):match("NvimTree") ~= nil then
			vim.cmd("quit")
		end
	end,
})

return {
	{
		-- 文件树（按命令延迟加载）
		"nvim-tree/nvim-tree.lua",
		cmd = { "NvimTreeToggle", "NvimTreeFocus" }, -- 只有执行这些命令时才加载
		config = function()
			-- nvim-tree 配置
			require("nvim-tree").setup({
				renderer = {
					icons = {
						show = {
							file = true, -- 不显示文件图标
							folder = true, -- 不显示文件夹图标
							folder_arrow = true, -- 不显示文件夹展开箭头
							git = true, -- 不显示git状态图标
						},
					},
				},
				view = {
					width = 40, -- 设置宽度为 40 个字符
				},
				update_focused_file = {
					enable = true, -- 启用自动聚焦
				},
				actions = {
					open_file = {
						quit_on_open = false,
					},
				},
			})
		end,
	},

	{
		"nvim-tree/nvim-web-devicons",
	},

	{
		"Yggdroot/LeaderF",
		init = function()
			vim.g.Lf_GitInlineBlameEnable = 1
		end,
		config = function()
			vim.api.nvim_set_keymap("n", "<C>p", ":LeaderfFile<CR>", { noremap = true, silent = true })
		end,
	},

	{
		"projekt0n/github-nvim-theme",
		name = "github-theme",
		lazy = false, -- make sure we load this during startup if it is your main colorscheme
		priority = 1000, -- make sure to load this before all the other start plugins
		config = function()
			require("github-theme").setup({
				options = {
					transparent = true,
				},
			})
            vim.cmd('colorscheme github_dark_colorblind')
            vim.cmd('hi CursorLine guibg=#3266d0 guifg=0')
		end,
	}
    ,
	{
		"tpope/vim-fugitive",
	},
	{
		"neoclide/coc.nvim",
		branch = "release",
		build = ":call coc#util#install()",
		ft = { "c", "cpp", "objc", "objcpp", "*" },
		config = function()
			-- Tab 键选中补全项
			-- vim.api.nvim_set_keymap('i', '<Tab>', 'coc#pum#visible() ? coc#pum#confirm()', { expr = true, noremap = true })
			-- vim.api.nvim_set_keymap('i', '<Enter>', 'coc#pum#visible() ? coc#pum#confirm()', { expr = true, noremap = true })
			vim.keymap.set("i", "<TAB>", function()
				return vim.fn["coc#pum#visible"]() == 1 and vim.fn["coc#pum#confirm"]() or "<TAB>"
			end, { expr = true, silent = true })

			vim.keymap.set("i", "<S-TAB>", function()
				return vim.fn["coc#pum#visible"]() == 1 and vim.fn["coc#pum#prev"](1) or "<C-h>"
			end, { expr = true })

			vim.keymap.set("i", "<CR>", function()
				return vim.fn["coc#pum#visible"]() == 1 and vim.fn["coc#pum#confirm"]() or "<CR>"
			end, { expr = true, silent = true })
		end,
	},
	{
		"rhysd/vim-clang-format",
	},
	-- {
	--     'romgrk/barbar.nvim',
	-- },
	-- plugins/tabby.lua 或直接放在你的插件配置中
	--
	{
		"nanozuki/tabby.nvim",
		event = "VimEnter",
		config = function()
			local theme = {
				fill = "TabLineFill",
				head = "TabLine",
				current_tab = "TabLineSel",
				tab = "TabLine",
				win = "TabLine",
				tail = "TabLine",
			}
			require("tabby.tabline").set(function(line)
				return {
					{
						{ " nvim ", hl = theme.head },
						line.sep("", theme.head, theme.fill),
					},
					line.tabs().foreach(function(tab)
						local hl = tab.is_current() and theme.current_tab or theme.tab
						return {
							line.sep("", hl, theme.fill),
							tab.is_current() and "" or "󰆣",
							tab.number(),
							tab.name(),
							tab.close_btn(""),
							line.sep("", hl, theme.fill),
							hl = hl,
							margin = " ",
						}
					end),
					line.spacer(),
					line.wins_in_tab(line.api.get_current_tab()).foreach(function(win)
						return {
							line.sep("", theme.win, theme.fill),
							win.is_current() and "" or "",
							win.buf_name(),
							line.sep("", theme.win, theme.fill),
							hl = theme.win,
							margin = " ",
						}
					end),
					{
						line.sep("", theme.tail, theme.fill),
						{ "  ", hl = theme.tail },
					},
					hl = theme.fill,
				}
			end)

			-- 键映射
			vim.keymap.set("n", "<leader>tn", ":tabnew<CR>", { desc = "New Tab" })
			vim.keymap.set("n", "<leader>tc", ":tabclose<CR>", { desc = "Close Tab" })
			vim.keymap.set("n", "<leader>to", ":tabonly<CR>", { desc = "Only This Tab" })
		end,
	},
    {
        'nvim-telescope/telescope.nvim', tag = '0.1.8',
        dependencies = { 'nvim-lua/plenary.nvim' }
    },
    {
        "nvim-treesitter/nvim-treesitter",
        branch = 'master',
        lazy = false,
        build = ":TSUpdate",
        config = function()
            require("nvim-treesitter").setup({
                ensure_installed = { "c", "cpp" },
                -- 启用语法高亮
                highlight = {
                    enable = true,           -- 启用高亮
                    disable = {},            -- 可禁用某些语言的高亮
                    additional_vim_regex_highlighting = false,  -- 禁用默认的 vim 正则高亮
                },
                -- 启用代码缩进
                indent = {
                    enable = true,           -- 启用智能缩进
                },
            })
		end,
    }
}
